import reg from 'service/config/reg.js'
import { mapState } from 'vuex'

var data = {
    pwd: '',
    smsCode: '',
    status: '',
    verifyauthsmsShow: false,
    verifyauthsmsCode: '',
    verifyauthsmsWaiting: false,
    verifyauthsmsText: '短信验证码',
    verifyauthsmsTimer: null
}

var vm = {
    data() {
        return data
    },
    computed: {
        ...mapState(['user']),
        // 图形验证码在手机号码输入正确的时候显示
        showAuthImg() {
            return reg.mobile.test(this.mobile)
        }
    },
    methods: {
        operatorLoginDo() {
            var pwd = this.pwd.trim()
            if (pwd == '') {
                $toast.show('请输入服务密码')
            } else {
                var operatorLoginPostData = {
                    userId: this.user.userId,
                    realName: this.user.realName,
                    idCard: this.user.idCard,
                    mobile: this.user.mobile,
                    pwd
                }
                $loading.show()
                $ajax.bqs.operatorLogin(operatorLoginPostData).then(res => {
                    var { code, msg, data } = res.data
                    $toast.show(msg)
                    if (code == 'S') {
                        this.status = data.status
                        if (this.status == 'CCOM1000') {
                            $router.replace('/loan/cacenter')
                        } else if (this.status == 'CCOM3069') {
                            // 自动发送一次短信
                            this.$refs.smsCode.wait()
                        } else if (this.status == 'CCOM3014') {
                            // 也会自动发送一次短信
                            this.verifyauthsmsWait()
                            this.verifyauthsmsShow = true
                        }
                    }
                }).catch(err => {
                    $toast.show('网络异常，请稍后重试')
                })
            }
        },
        getSmsAuth() {
            this.verifyauthsmsWaiting = true
            $ajax.bqs.operatorSendsms({
                userId: this.user.userId,
                smsType: 'auth'
            }).then(res => {
                var { code, msg } = res.data
                $toast.show(msg)
                if (code == 'S') {
                    this.verifyauthsmsWait()
                } else {
                    this.verifyauthsmsWaiting = false
                }
            }).catch(err => {
                $toast.show('网络异常，请稍后重试')
                this.verifyauthsmsWaiting = true
            })
        },
        getSmsLogin() {
            $ajax.bqs.operatorSendsms({
                userId: this.user.userId,
                smsType: 'login'
            }).then(res => {
                var { code, msg } = res.data
                $toast.show(msg)
                if (code == 'S') {
                    this.$refs.smsCode.wait()
                } else {
                    this.$refs.smsCode.clearWait()
                }
            }).catch(err => {
                $toast.show('网络异常，请稍后重试')
                this.$refs.smsCode.clearWait()
            })
        },
        verifyauthsmsDo() {
            var smsCode = this.verifyauthsmsCode.trim()
            if (smsCode == '') {
                $toast.show('请输入二次鉴权验证码')
                return
            }
            $loading.show()
            $ajax.bqs.operatorVerifyauthsms({
                userId: this.user.userId,
                smsCode
            }).then(res => {
                var { code, msg } = res.data
                $toast.show(msg)
                if (code == 'S') {
                    $router.replace('/loan/cacenter')
                }
            }).catch(err => {
                $toast.show('网络异常，请稍后重试')
            })
        },
        verifyauthsmsWait() {
            var seconds = 60
            this.verifyauthsmsWaiting = true
            this.verifyauthsmsText = seconds + 's'
            clearInterval(this.verifyauthsmsTimer)
            this.verifyauthsmsTimer = setInterval(() => {
                seconds--
                if (seconds <= 0) {
                    this.verifyauthsmsText = '获取验证码'
                    this.verifyauthsmsWaiting = false
                    clearInterval(this.verifyauthsmsTimer)
                } else {
                    this.verifyauthsmsText = seconds + 's'
                }
            }, 1000)
        }
    },
    mixins: [$mixins.needLogin]
}

export default vm